package com.jmxcfc.blfsc.common.validation.rule;

import cn.hutool.core.util.IdcardUtil;
import cn.hutool.core.util.PhoneUtil;
import com.jmxcfc.blfsc.common.common.ComConstants;
import com.jmxcfc.blfsc.common.sign.SignConstants;
import com.jmxcfc.blfsc.common.sign.enums.SignType;
import com.jmxcfc.blfsc.common.validation.IRuleValidator;
import com.jmxcfc.blfsc.common.validation.RegexRuleValidator;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.Objects;

/**
 * 签章模块的规则校验规则
 */
@Slf4j
public class SignRulesUtil {

    private SignRulesUtil() {
        throw new IllegalStateException("Utility class");
    }

    /**
     * 流水号字段校验:8位时间+32为uuid
     * @return
     */
    public static RequestFieldRule getRequestNoRule(){
        //return RequestFieldRule.of(ComConstants.REQUEST_NO,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(ComConstants.REQUEST_NO,"流水号字段规则检验不符合"));

        String requestNoRegex = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})(((0[13578]|1[02])(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)(0[1-9]|[12][0-9]|30))|(02(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))0229))\\w{32}$";
        return RequestFieldRule.of(ComConstants.REQUEST_NO,
                RegexRuleValidator.of(requestNoRegex),  StringUtils.join(ComConstants.REQUEST_NO,"业务请求号字段规则检验不符合"));

    }


    /**
     * 请求流水号(基础服务平台生成)
     94语音对应requestId字段的概念
     玄武对应batchId的概念
     集微对应transactionId字段的概念
     CFCA对应serialNo(单笔业务流水号)字段的概念
     * @return
     */
    public static RequestFieldRule getSerialSeqRule(String errorDesc){
        return RequestFieldRule.of(ComConstants.SERIAL_SEQ,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(ComConstants.SERIAL_SEQ,StringUtils.isBlank(errorDesc)?"业务流水号字段规则检验不符合":errorDesc));
    }


    /**
     * 产品编号字段:不为空校验
     * @return
     */
    public static RequestFieldRule getProdCodeRule(){
        return RequestFieldRule.of(SignConstants.PROD_CODE,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.PROD_CODE,"产品编号字段规则检验不符合"));
    }

    /**
     * 用户名字段:不为空校验
     * @return
     */
    public static RequestFieldRule getAcctNameRule(){
        return RequestFieldRule.of(SignConstants.ACCT_NAME,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.ACCT_NAME,"用户名字段规则检验不符合"));
    }

    /**
     * 证件号字段:不为空校验
     * @return
     */
    public static RequestFieldRule getIdCodeRule(){
        return RequestFieldRule.of(SignConstants.ID_CODE, IdcardUtil::isValidCard, StringUtils.join(SignConstants.ID_CODE,"证件号字段规则检验不符合"));
    }

    /**
     * 待签章文件pdf的路径字段不为空校验
     * @return
     */
    public static RequestFieldRule getTemplePdfPathRule(){
        return RequestFieldRule.of(SignConstants.TEMPLE_PDF_PATH,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.TEMPLE_PDF_PATH,"待签章文件pdf的路径字段规则检验不符合"));
    }

    /**
     * 扩展参数:签署类型字段:不为空校验
     * @return
     */
    public static RequestFieldRule getSignTypeRule(){
        return RequestFieldRule.of(SignConstants.SIGN_TYPE, (IRuleValidator<String>) value -> Objects.nonNull(SignType.getEnumByType(value)), StringUtils.join(SignConstants.SIGN_TYPE, "扩展参数:签署类型字段规则检验不符合(超出签署类型的枚举)"));
    }


    /**
     * 电话号码校验
     * @return
     */
    public static RequestFieldRule getPhoneNoRule(){
        return RequestFieldRule.of(SignConstants.PHONE_NO, PhoneUtil::isMobile, StringUtils.join(SignConstants.PHONE_NO,"电话号码字段规则检验不符合"));
    }

    /**
     *  企业章账户校验
     * @return
     */
    public static RequestFieldRule getSealAccountRule(){
        return RequestFieldRule.of(SignConstants.SEAL_ACCOUNT,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.SEAL_ACCOUNT,"扩展参数:企业章账号字段规则检验不符合"));
    }


    /**
     * 企业章密码规则
     * @return
     */
    public static RequestFieldRule getSealPasswordRule(){
        return RequestFieldRule.of(SignConstants.SEAL_PASSWORD,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.SEAL_PASSWORD,"扩展参数:企业章密码字段规则检验不符合"));
    }



    /**
     * 企业章图片路径
     * @return
     */
    public static RequestFieldRule getImagePathRule(){
        return RequestFieldRule.of(SignConstants.IMAGE_PATH,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.IMAGE_PATH,"扩展参数:企业章图片路径字段规则检验不符合"));
    }

    /**
     * 合同模板页数
     * @return
     */
    public static RequestFieldRule getPageNumRule(){
        return RequestFieldRule.of(SignConstants.PAGE_NUM,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.PAGE_NUM,"扩展参数:合同模板页数字段规则检验不符合"));
    }


    /**
     * 签章位置的页码(个人章)
     * @return
     */
    public static RequestFieldRule getSignPageRule(){
        return RequestFieldRule.of(SignConstants.SIGN_PAGE,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.SIGN_PAGE,"扩展参数:签章位置的页码(个人章)字段规则检验不符合"));
    }


    /**
     * 签署X坐标(个人)
     * @return
     */
    public static RequestFieldRule getSignXRule(){
        return RequestFieldRule.of(SignConstants.SIGN_X,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.SIGN_X,"扩展参数:签署X坐标(个人)字段规则检验不符合"));
    }

    /**
     * 签署Y坐标(个人)
     * @return
     */
    public static RequestFieldRule getSignYRule(){
        return RequestFieldRule.of(SignConstants.SIGN_Y,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.SIGN_Y,"扩展参数:签署Y坐标(个人)字段规则检验不符合"));
    }


    /**
     * 签章位置的页码(企业章)
     * @return
     */
    public static RequestFieldRule getSignPageEntRule(){
        return RequestFieldRule.of(SignConstants.SIGN_PAGE_ENT,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.SIGN_PAGE_ENT,"扩展参数:签章位置的页码(企业章)字段规则检验不符合"));
    }



    /**
     * 签署X坐标(企业章)
     * @return
     */
    public static RequestFieldRule getSignXEntRule(){
        return RequestFieldRule.of(SignConstants.SIGN_X_ENT,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.SIGN_X_ENT,"扩展参数:签署X坐标(企业章)字段规则检验不符合"));
    }

    /**
     * 签署Y坐标(企业章)
     * @return
     */
    public static RequestFieldRule getSignYEntRule(){
        return RequestFieldRule.of(SignConstants.SIGN_Y_ENT,(IRuleValidator<String>) StringUtils::isNotBlank, StringUtils.join(SignConstants.SIGN_Y_ENT,"扩展参数:签署Y坐标(企业章)字段规则检验不符合"));
    }

}
